package user_api

import (
	"github.com/gogf/gf/frame/g"
)

// 主用户模块
const (
	OPEN_API_USER_NAME_SHOW          = "user.master.show"
	OPEN_API_USER_NAME_FIND_BY_EMAIL = "user.master.findByEmail"
	OPEN_API_USER_NAME_LIST          = "user.master.list"
	OPEN_API_USER_NAME_CREATE        = "user.master.create"
	OPEN_API_USER_NAME_UPDATE        = "user.master.update"
	OPEN_API_USER_NAME_DELETE        = "user.master.delete"
	OPEN_API_USER_NAME_PASSWORD      = "user.master.password"
	OPEN_API_USER_NAME_ACCESSES      = "user.master.accesses"
	OPEN_API_USER_NAME_ACCESSES_SAVE = "user.master.accesses.save"
)

// 查询指定用户，根据用户邮箱
func FindByEmail(email string) string {
	return CallApi(OPEN_API_USER_NAME_FIND_BY_EMAIL, g.Map{
		"api_uri_placeholder_values": g.Map{
			"email": email,
		},
	})
}

// 获取用户列表
func GetUserList(params map[string]interface{}) string {
	return CallApi(OPEN_API_USER_NAME_LIST, params)
}

// 创建用户
func CreateUser(params map[string]interface{}) string {
	return CallApi(OPEN_API_USER_NAME_CREATE, g.Map{
		"email":    params["email"],
		"name":     params["name"],
		"note":     params["note"],
		"password": params["password"],
		"status":   params["status"],
	})
}

// 编辑用户
func UpdateUser(id string, params map[string]interface{}) string {
	return CallApi(OPEN_API_USER_NAME_UPDATE, g.Map{
		"api_uri_placeholder_values": g.Map{
			"id": id,
		},
		"name":   params["name"],
		"note":   params["note"],
		"status": params["status"],
	})
}

// 删除用户
func DeleteUser(id string) string {
	return CallApi(OPEN_API_USER_NAME_DELETE, g.Map{
		"api_uri_placeholder_values": g.Map{
			"id": id,
		},
	})
}

// 重置密码
func ResetPassword(id string, params map[string]interface{}) string {
	return CallApi(OPEN_API_USER_NAME_PASSWORD, g.Map{
		"api_uri_placeholder_values": g.Map{
			"id": id,
		},
		"password":              params["password"],
		"password_current":      params["password_current"],
		"password_confirmation": params["password_confirmation"],
	})
}

// 获取用户权限
func GetAccesses(id string, perLine int) string {
	return CallApi(OPEN_API_USER_NAME_ACCESSES, g.Map{
		"api_uri_placeholder_values": g.Map{
			"id": id,
		},
		"perLine": perLine,
	})
}

// 保存用户权限
func SaveAccesses(id string, accessIds []string, isChild bool) string {
	return CallApi(OPEN_API_USER_NAME_ACCESSES_SAVE, g.Map{
		"api_uri_placeholder_values": g.Map{
			"id": id,
		},
		"accessIds": accessIds,
		"is_child": isChild,
	})
}
